package com.example.taximeter.config;

import com.example.taximeter.bean.FileInfo;
import com.example.taximeter.bean.ReturnInfo;
import com.example.taximeter.bean.User;
import com.example.taximeter.bean.UserLog;
import com.example.taximeter.mapper.CompanyMapper;
import com.example.taximeter.mapper.RoleMapper;
import com.example.taximeter.mapper.UserMapper;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import javax.xml.crypto.Data;
import java.io.File;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GlobalParam {

    //查询日期格式转换
    public static String changeRiqi(String riqi){
        if(riqi.length()==6){
            StringBuilder stringBuilder = new StringBuilder(riqi);
            stringBuilder.insert(4,"-");
            return stringBuilder.toString();
        }
        if(riqi.length()==8){
            StringBuilder stringBuilder = new StringBuilder(riqi);
            stringBuilder.insert(4,"-");
            stringBuilder.insert(7, "-");
            return stringBuilder.toString();
        }
        return riqi;
    }

    //String-->BigDecimal-->*-
    public static BigDecimal toBigDecimalMultiply (String str1, String str2){
        BigDecimal bd1 =  new BigDecimal(str1);
        BigDecimal bd2 =  new BigDecimal(str2);
        return bd1.multiply(bd2);
    }

    //String-->BigDecimal-->*-
    public static BigDecimal toBigDecimalAdd (String str1, String str2, String str3){
        BigDecimal bd1 =  new BigDecimal(str1);
        BigDecimal bd2 =  new BigDecimal(str2);
        BigDecimal bd3 =  new BigDecimal(str3);
        return bd1.add(bd2).add(bd3);
    }

    public static BigDecimal toBigDecimalMultiply (Integer str1, String str2){
        BigDecimal bd1 =  new BigDecimal(str1);
        BigDecimal bd2 =  new BigDecimal(str2);
        return bd1.multiply(bd2);
    }

    //获取Date时间
    public static Date getTime(){
        Date date = new Date();
        return new Timestamp(date.getTime());
    }
    //获取Date时间
    public static Date getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(0);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }

    public static Date getNTime(int n){
        Calendar cal= Calendar.getInstance();
        cal.add(Calendar.DATE,n);
        return cal.getTime();
    }

    public static String dateToString(Date date){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }

    public static Date getNnTime(Date date, int n){
        Calendar cal= Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE,n);
        return cal.getTime();
    }

    //获取String日期n天前后，格式yyyy-MM-dd
    public static String yyMMdd(int n){
        Calendar cal= Calendar.getInstance();
        cal.add(Calendar.DATE,n);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    //获取String日期n天前后，格式yyyy-MM
    public static String yyMM(int n){
        Calendar cal= Calendar.getInstance();
        cal.add(Calendar.DATE,n);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM").format(time);
    }

    //获取String日期--yyyyMMdd
    public static String yyyyMMdd(){
        Date date=new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyyMMdd");
        return dateFm.format(date);
    }

    //获取String日期--yyyyMMdd
    public static String yyyyMMdd2(){
        Date date=new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
        return dateFm.format(date);
    }

    //获取String日期--yyyyMMdd
    public static String yyyyMMddHHmmss(){
        Date date=new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFm.format(date);
    }

    //获取String日期--dd
    public static String dd(){
        Date date=new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("dd");
        return dateFm.format(date);
    }

    //获取String日期--yyyyMMdd
    public static String yyyyMM(){
        Date date=new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyyMM");
        return dateFm.format(date);
    }

    //获取String日期--yyyyMMdd
    public static String yyyyMM2(){
        Date date=new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM");
        return dateFm.format(date);
    }

    //获取UUID
    public static String getUUID(){
        return UUID.randomUUID().toString().replaceAll("-","");
    }

    //汉字转拼音首写字母（小写）
    public static String toPinyin(String string){
        StringBuilder convert = new StringBuilder("");
        int len = string.length();
        for (int j = 0; j < len; j++) {
            char word = string.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert.append(pinyinArray[0].charAt(0));
            } else {
                convert.append(word);
            }
        }
        return convert.toString();
    }

    //返回错误信息
    public static List<String> toErrors(BindingResult result){
        List<String> errors = new ArrayList<>();
        List<ObjectError> objectErrors = result.getAllErrors();
        for(ObjectError objectError : objectErrors){
            errors.add(objectError.getDefaultMessage());
        }
        return errors;
    }

    //密码MD5加密
    public static String useMD5(String str) throws NoSuchAlgorithmException {
        // 生成一个MD5加密计算摘要
        MessageDigest md = MessageDigest.getInstance("MD5");
        // 计算md5函数
        md.update(str.getBytes());
        byte b[] = md.digest();
        int i;
        StringBuilder buf = new StringBuilder("");
        for (byte aB : b) {
            i = aB;
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        return buf.toString();
    }

    //删除文件
    public static ReturnInfo deleteF(FileInfo fileInfo, String path){
        File file = new File(path, fileInfo.getFileurl());
        if(file.exists()){
            if(!file.delete()){
                return ReturnInfo.info1("删除失败！");
            }
            return ReturnInfo.info1("删除成功！");
        }else {
            return ReturnInfo.info1("文件不存在！");
        }
    }

    //检验日期格式
    public static Boolean chickData(String str){
        String chick = "(^[0-9]{4}-[0-9]{2}(-[0-9]{2})?$)|(^$)";
        Pattern p = Pattern.compile(chick);
        Matcher m = p.matcher(str);
        return m.matches();
    }
}
